package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cn.yunhe.entity.User;

/**
 * Servlet implementation class Ajax_json
 */
@WebServlet("/Ajax_json")
public class Ajax_json extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ajax_json() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				PrintWriter out= response.getWriter();
				User user=new User();
				List<User> list=new ArrayList<User>();
				list.add(new User(1001,"朱老大","123"));
				list.add(new User(1002,"朱老二","321"));
				list.add(new User(1003,"朱老三","231"));
				/*StringBuffer strJson=new StringBuffer();
				strJson.append("[");
				for (User user2 : list) {
					strJson.append("{");
					strJson.append("\"id\":"+user2.getId()+",\"loginName\":"+user2.getLoginName()+",\"pwd\":"+user2.getPwd());
					strJson.append("}");
					strJson.append(",");
				}
				strJson.deleteCharAt(strJson.length()-1);
				strJson.append("]");
				out.write(strJson.toString());
				out.close();*/
				Gson g=new Gson();
				String JsonStr=g.toJson(list);
				out.write(JsonStr);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/*public static void main(String[] args) {
		User user=new User();
		List<User> list=new ArrayList<User>();
		list.add(new User(1001,"朱老大","123"));
		list.add(new User(1002,"朱老二","321"));
		list.add(new User(1003,"朱老三","231"));
		StringBuffer strJson=new StringBuffer();
		strJson.append("[");
		for (User user2 : list) {
			strJson.append("{");
			strJson.append("\"id\":"+user2.getId()+",\"loginName\":\""+user2.getLoginName()+"\",\"pwd\":"+user2.getPwd());
			strJson.append("}");
			strJson.append(",");
		}
		strJson.deleteCharAt(strJson.length()-1);
		strJson.append("]");
		System.out.println(strJson.toString());
	}*/
	
}
